package com.lanyam.travel.base.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanyam.travel.base.entity.vo.DistrUserVo;
import com.lanyam.travel.base.entity.vo.TravelOrderVo;
import com.lanyam.travel.base.service.ITravelOrderService;
import com.lanyam.travel.base.utils.JSONResult;
import com.lanyam.travel.base.entity.DistrRecord;
import com.lanyam.travel.base.entity.vo.DistrRecordVo;
import com.lanyam.travel.base.service.IDistrRecordService;
import com.lanyam.travel.base.utils.JsonMapper;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.core.type.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;


@RestController
@RequestMapping("/distrRecord")
public class DistrRecordController {

    @Autowired
    private IDistrRecordService distrRecordService;

    /**
     * 分页查询数据
     */
    @GetMapping("/getPage")
    public IPage<DistrRecordVo> getPage (String queryMap,
                                                @RequestParam(name="page", defaultValue = "1") Integer page,
                                                @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        IPage<DistrRecordVo> pageInfo = distrRecordService.getPageDistrRecord(query, page, rows);
        return pageInfo;
    }

    /**
     * 微信端查询
     * @param reqMap
     * @return
     */
    @RequestMapping(value="/query", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JSONResult query (@RequestBody(required = false) Map<String,Object> reqMap) {
        Map<String,Object> map = (Map<String, Object>) reqMap.get("reqMap");
        Integer pageNum = Integer.parseInt(map.get("page").toString());
        Integer pageSize = Integer.parseInt(map.get("pageSize").toString());
        String userId = map.get("userId").toString();
        IPage<DistrRecordVo> pageInfo = distrRecordService.getPageDistrRecord(map, pageNum, pageSize);
        return JSONResult.ok(pageInfo);
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public JSONResult add (@RequestBody DistrRecord distrRecord) {
        JSONResult result = null;
        try{
            distrRecordService.addDistrRecord(distrRecord);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 编辑数据
     */
    @PostMapping("/update")
    public JSONResult update (@RequestBody DistrRecord distrRecord) {
        JSONResult result = null;
        try{
            distrRecordService.updateDistrRecord(distrRecord);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 删除数据
     */
    @PostMapping("/delById")
    public JSONResult delById (String id) {
        JSONResult result = null;
        try {
            distrRecordService.delById(id);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }


    /**
     * 计算分销佣金 and 记录入库 and 改变分销商余额
     *
     */
    @PostMapping("/getCommission")
    public JSONResult getCommission (String distrUserId, BigDecimal orderMoney, String orderId) {
        JSONResult result = null;
        try {
            result = distrRecordService.getCommission(distrUserId,orderMoney,orderId);
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 获取分销团队成员
     * @param teamId
     * @return
     */
    @RequestMapping(value = "/getDistrRecordByTeamId", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public JSONResult getDistrRecordByTeamId(String teamId){
        List<DistrRecordVo> userVos = distrRecordService.getDistrRecordByTeamId(teamId);
        return JSONResult.ok(userVos);
    }

}